package com.alibaba.citrus.cr.price.ability.functions.entity.price;

import com.alibaba.citrus.cr.common.util.SpringContextUtil;
import com.alibaba.citrus.cr.price.repository.PriceQueryRepository;
import com.alibaba.citrus.cr.price.repository.PriceWriteRepository;
import com.epoch.app.crpricecenter.sdo.PriceDimensionConfigSDO;
import lombok.Data;

import java.util.Date;
import java.util.Objects;

/**
 * @author: qiaojiange
 * @create: 2021/09/15
 * @Description 价格维度配置实体
 */
@Data
public class PriceDimensionConfigEntity {

    private PriceDimensionConfigSDO priceDimensionConfigSDO;

    private static PriceWriteRepository priceWriteRepository;


    private static PriceQueryRepository priceQueryRepository;

    static {
        priceWriteRepository = SpringContextUtil.getBean(PriceWriteRepository.class);
        priceQueryRepository = SpringContextUtil.getBean(PriceQueryRepository.class);
    }

    public static PriceDimensionConfigEntity of(PriceDimensionConfigSDO dimensionConfigSDO) {
        PriceDimensionConfigEntity entity = new PriceDimensionConfigEntity();
        entity.setPriceDimensionConfigSDO(dimensionConfigSDO);
        return entity;
    }

    /**
     * 获取记录编码
     *
     * @return
     */
    public String obtainRecodeId() {
        return thisSdoIsNull() ? "" : priceDimensionConfigSDO.getPriceRecordId();
    }

    /**
     * 判断SDO 是否为 null? true：为空，false: 不为空；
     *
     * @return
     */
    private boolean thisSdoIsNull() {
        return this.priceDimensionConfigSDO == null;
    }

    /**
     * 这个价格数据是否可用
     *
     * @param priceUseDate 价格使用时间
     * @return true: 可以使用， false :不可以使用
     */
    public boolean isCanUse(Date priceUseDate) {
        return !this.thisSdoIsNull()
                && priceDimensionConfigSDO.getPriceStartDate().before(priceUseDate)
                && priceDimensionConfigSDO.getPriceEndDate().after(priceUseDate);
    }

    /**
     * 保存
     *
     * @return
     */
    public Boolean save() {
//        todo qjg 这里需要查看下
        PriceDimensionConfigSDO priceDimensionConfigSDO = priceQueryRepository.queryPriceDimensionConfig(this.priceDimensionConfigSDO);
        boolean result = false;
        if (Objects.nonNull(priceDimensionConfigSDO) && Objects.nonNull(priceDimensionConfigSDO.getId())) {
            this.priceDimensionConfigSDO.setVersion(priceDimensionConfigSDO.getVersion());
            result = priceWriteRepository.updatePriceDimensionConfig(priceDimensionConfigSDO.getId(), this.priceDimensionConfigSDO);
        } else {
            result = priceWriteRepository.savePriceDimensionConfig(this.priceDimensionConfigSDO);
        }
        return result;
    }

    /**
     * 维度id
     * @return
     */
    public String obtainDimensionId() {
        return this.thisSdoIsNull()? "": this.priceDimensionConfigSDO.getDimensionId();
    }

    /**
     * 获取价格使用的开始时间
     * @return
     */
    public Date obtainPriceStartDate() {
        return this.thisSdoIsNull()?null:this.getPriceDimensionConfigSDO().getPriceStartDate();

    }

    /**
     * 获取价格使用的结束时间
     * @return
     */
    public Date obtainPriceEndDate() {
        return this.thisSdoIsNull()?null:this.getPriceDimensionConfigSDO().getPriceEndDate();

    }

    /**
     * 获取md5Code
     * @return
     */
    public String obtainMd5Code(){
        return this.thisSdoIsNull()?null:getPriceDimensionConfigSDO().getMd5Code();
    }

    public String obtainPriceType() {
        return this.thisSdoIsNull() ? null : this.getPriceDimensionConfigSDO().getPriceTypeId();
    }
}
